local love = require 'love'

local player
local obstacles
local obstacleImages
local obstacleSpawnTimer
local score
local obstacleSpawnInterval
local obstacleSpeed

-- 初始化函数（游戏启动时调用一次）
function love.load()
    -- 加载玩家图片
    player = {}
    player.image = love.graphics.newImage('assets/player.png')
    if not player.image then
        print('玩家图片加载失败，请检查文件路径和文件是否存在。')
    end
    -- 获取玩家图片的宽度和高度
    player.width = player.image:getWidth()
    player.height = player.image:getHeight()
    -- 可调整碰撞检测区域
    player.collisionWidth = player.width * 0.2
    player.collisionHeight = player.height * 0.2
    player.x = 400
    player.y = 500
    player.speed = 300

    -- 加载障碍物图片
    obstacleImages = {}
    local files = love.filesystem.getDirectoryItems('assets')
    for _, file in ipairs(files) do
        if string.find(file, '^obstacle_') then
            local image = love.graphics.newImage('assets/' .. file)
            if image then
                table.insert(obstacleImages, image)
            else
                print('障碍物图片 ' .. file .. ' 加载失败，请检查文件路径和文件是否存在。')
            end
        end
    end

    if #obstacleImages == 0 then
        print('没有找到有效的障碍物图片，请检查 assets 目录。')
    end

    obstacles = {}
    score = 0
    obstacleSpawnTimer = 0
    obstacleSpawnInterval = 1 -- 障碍物生成间隔，单位：秒
    obstacleSpeed = 200 -- 障碍物移动速度
end

-- 碰撞检测函数，增加参数以支持调整碰撞区域
local function checkCollision(a, b, obsCollisionWidth, obsCollisionHeight)
    return a.x + (a.width - a.collisionWidth) / 2 < b.x + obsCollisionWidth and
           a.x + (a.width - a.collisionWidth) / 2 + a.collisionWidth > b.x and
           a.y + (a.height - a.collisionHeight) / 2 < b.y + obsCollisionHeight and
           a.y + (a.height - a.collisionHeight) / 2 + a.collisionHeight > b.y
end

-- 更新函数（每帧调用）
function love.update(dt)
    -- 玩家移动
    if love.keyboard.isDown('right') and player.x < 800 - player.width then
        player.x = player.x + player.speed * dt
    elseif love.keyboard.isDown('left') and player.x > 0 then
        player.x = player.x - player.speed * dt
    elseif love.keyboard.isDown('up') and player.y > 0 then
        player.y = player.y - player.speed * dt
    elseif love.keyboard.isDown('down') and player.y < 600 - player.height then
        player.y = player.y + player.speed * dt
    end

    -- 障碍物生成
    obstacleSpawnTimer = obstacleSpawnTimer + dt
    if obstacleSpawnTimer >= obstacleSpawnInterval then
        if #obstacleImages > 0 then
            local randomIndex = love.math.random(#obstacleImages)
            local obsImage = obstacleImages[randomIndex]
            table.insert(obstacles, {
                x = math.random(0, 800 - obsImage:getWidth()),
                y = -obsImage:getHeight(),
                image = obsImage
            })
        end
        obstacleSpawnTimer = 0
    end

    -- 障碍物移动和碰撞检测
    for i = #obstacles, 1, -1 do
        local obs = obstacles[i]
        obs.y = obs.y + obstacleSpeed * dt
        -- 可调整障碍物碰撞检测区域
        local obsCollisionWidth = obs.image:getWidth() * 0.15
        local obsCollisionHeight = obs.image:getHeight() * 0.7

        -- 碰撞检测
        if checkCollision(player, obs, obsCollisionWidth, obsCollisionHeight) then
            love.event.quit()
        end

        -- 移除超出屏幕的障碍物并增加得分
        if obs.y > 600 then
            table.remove(obstacles, i)
            score = score + 1
        end
    end
end

-- 绘制函数（每帧调用）
function love.draw()
    -- 绘制玩家图片
    love.graphics.draw(player.image, player.x, player.y)
    -- 绘制障碍物图片
    for _, obs in ipairs(obstacles) do
        love.graphics.draw(obs.image, obs.x, obs.y)
    end

    -- 显示得分
    love.graphics.setColor(1, 1, 1)
    love.graphics.print('Score: ' .. score, 10, 10)
end